package com.springboot.codeminestarter.codemine.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

/**
 * Spring Bean LifeCycle
 */
public class UserBean implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {

    private String id;
    private String name;

    public UserBean() {
        System.out.println("构造方法实例化UserBean");
    }

    public UserBean(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public void setId(String id) {
        this.id = id;
        System.out.println("设置id属性");
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("设置name属性");
    }

    @Override
    public String toString() {
        return "UserBean{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("设置Bean name:" + name);
    }


    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("设置BeanFactory：" + beanFactory);
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println("属性注入完成，执行afterPropertiesSet方法");
    }

    @Override
    public void destroy() {
        System.out.println("执行Bean的销毁方法");
    }

    public void handleBean() {

        System.out.println("执行Bean自定义方法HandleBean");
    }

    /**
     * init method
     */
    public void init () {
        System.out.println("自定义初始化方法");
    }

    /**
     * destroy method
     */
    public void destroyMethod () {
        System.out.println("自定义销毁方法");
    }
}
